﻿/*-------------------------------------
//
//CopyLeft 2007 开发二组 
//
//文件名：PrintReceiptOrder.cs
//
//文件功能描述：收款单打印.
// 
//创建标识：2007.11.21 曾健铭
//
//修改标识: 2007.11.28 曾健铭
//修改描述: 修正Bug,按规范重新编码..
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class ReceiptMenoyOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strReceiptID = string.Empty;
        //string strReceiptID = "REC-2007103010000";
        try
        {
            strReceiptID = Request["ReceiptID"].ToString();
            string strPattern = @"REC-\d{13}";
            if((strReceiptID.Length !=17) || (!Regex.Match(strReceiptID,strPattern).Success))
            {
                strReceiptID = string.Empty;
            }
        }
        catch
        {
            strReceiptID = string.Empty;
        }
        if(strReceiptID == string.Empty)
        {
            lblInfo.Text = "传入的ID号有错误";
        }
        else
        {
            //绑定数据到Label中
            BindDataToLabel(strReceiptID);

            string strOrderInfoID = Session["OrderInfoID"].ToString();
            //由订单号获取订单项
            //string strOrderInfoID = "ORD-2007103010003";
            CallWebService(strOrderInfoID);//异步调用
        }
    }

    /// <summary>
    /// 由发货单号获取发货单的信息,将数据绑定到Label
    /// </summary>
    /// <param name="strReceiptID"></param>
    private void BindDataToLabel(string strReceiptID)
    {
        localhost.V_Receipt receipt = new localhost.V_Receipt();
        receipt = (new localhost.DocumentService()).GetReceiptDetail(strReceiptID);
        if (receipt == null)
        {
            lblInfo.Text = "收款单不存在";
        }
        else
        {
            //收货人信息.
            lblBillName.Text = receipt.BillToFirstName + receipt.BillToLastName;//姓名
            //收货单信息.
            lblReceiptID.Text = receipt.ReceiptID;//发货单号

            lblOrderInfoID.Text = receipt.OrderInfoID;//订单号
            Session["OrderInfoID"] = lblOrderInfoID.Text;

            //lblBillName.Text = receipt.
            int iSalesmanID = receipt.SalesmanID;
            lblSalesmanName.Text = (new localhost.StaffService()).GetStaffNameByID(iSalesmanID);//发货人姓名
            lblBillMethod.Text = receipt.BillMethod;//付款方式
            lblCreationTime.Text = receipt.CreationTime.ToString();//发货日期
            lblPhone.Text = (new localhost.DocumentService()).GetOrderDetail(lblOrderInfoID.Text).Phone;//电话号码
            lblTotalPrice.Text = (new localhost.DocumentService()).GetOrderDetail(lblOrderInfoID.Text).TotalPrice.ToString();//收款单总价
        }

    }
    /// <summary>
    /// 异步调用,获取订单项
    /// </summary>
    /// <param name="strOrderInfoID"></param>
    private void CallWebService(string strOrderInfoID)
    {
        localhost.DocumentService service = new localhost.DocumentService();
        service.GetOrderItemCompleted += new localhost.GetOrderItemCompletedEventHandler(service_GetOrderItemCompleted);//声明回调函数
        service.GetOrderItemAsync(strOrderInfoID);
    }
    /// <summary>
    /// 回调函数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void service_GetOrderItemCompleted(object sender, localhost.GetOrderItemCompletedEventArgs e)
    {
        //将数据绑定到GridView.
        gdvOrderItem.DataSource = null;
        if (e.Result.Tables.Count != 0)
        {
            gdvOrderItem.DataSource = e.Result.Tables[0];
        }
        gdvOrderItem.DataBind();
        lblQuantity.Text = gdvOrderItem.Rows.Count.ToString();//获取物品的总件数     
    }
}
